{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b7ee20f",
   "metadata": {},
   "source": [
    "### 字典键值对\n",
    "\n",
    "* Python字典的键部分通常是一个字符串，关联的值部分可以是任意的python对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "31642af2",
   "metadata": {},
   "outputs": [],
   "source": [
    "person3 = {'name':'Lin Zhiyu',\n",
    "           'Gender':'Male',\n",
    "           'Occupation':'Researcher',\n",
    "           'Home Planet':'Betelgeuse Seven'\n",
    "          }\n",
    "# 这种就是字典里的键与值（冒号：左边为键，右边对值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "001df5fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Lin Zhiyu',\n",
       " 'Gender': 'Male',\n",
       " 'Occupation': 'Researcher',\n",
       " 'Home Planet': 'Betelgeuse Seven'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32c97c9d",
   "metadata": {},
   "source": [
    "### 字典不会维持插入顺序\n",
    "\n",
    "* 列表会维持对象插入时的顺序，与列表不同，python的字典不会这么做。它们是无序的。\n",
    "\n",
    "但其实顺序并没有太大的影响，选择字典中存储的数据时，与字典的顺序毫无关系，关键在于你使用的键。\n",
    "要用键来查找值。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8bce602",
   "metadata": {},
   "source": [
    "### 中括号\n",
    "\n",
    "列表使用数值索引值来访问数据，字典使用键来访问其关联的数据值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ef23327b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Lin Zhiyu'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3['name']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69399713",
   "metadata": {},
   "source": [
    "### 运行时处理字典\n",
    "\n",
    "#### 在字典中增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c9996f11",
   "metadata": {},
   "outputs": [],
   "source": [
    "person3['Age'] = 19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b45a9af6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Lin Zhiyu',\n",
       " 'Gender': 'Male',\n",
       " 'Occupation': 'Researcher',\n",
       " 'Home Planet': 'Betelgeuse Seven',\n",
       " 'Age': 19}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f0d9985",
   "metadata": {},
   "source": [
    " ##### 选择一个频度计数数据结构\n",
    " 先创建一个空字典，再为每个元音创建一行，并将其关联的值初始化为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dd404248",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c176a03b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3a5fd686",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新频度计数器\n",
    "\n",
    "found\n",
    "found['e'] = found['e']+1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0fae5270",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新频度计数器 2.0版本\n",
    "found['e'] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "92e10f08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s.)\n",
      "e was found 2 time(s.)\n",
      "i was found 0 time(s.)\n",
      "o was found 0 time(s.)\n",
      "u was found 0 time(s.)\n"
     ]
    }
   ],
   "source": [
    " # for循环迭代处理一个字典时，解释器只处理字典的键key\n",
    " #   我们可以使用found[k]访问它的关联值，并向print函数调用传入两个字符串来生成更友好的输出\n",
    "\n",
    "for k in found:\n",
    "    print(k,'was found',found[k],'time(s.)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3c795109",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "# 指定输出时的字典顺序\n",
    "# sorted()函数\n",
    "\n",
    "for k in sorted(found):\n",
    "    print(k,'was found',found[k],'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7358f18d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "# items()方法 它会返回一个键值对列表\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9805b4fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:happy\n",
      "a was found 1 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input('Provide a word to search for vowels:')\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "        for k,v in sorted(found.items()):\n",
    "            print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c092e66e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:fruit\n",
      "u was found 1 time(s).\n",
      "i was found 1 time(s).\n",
      "u was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "# setdefault 方法\n",
    "# 可以把一个不存在的键初始化为一个指定的默认值，或什么也不做（已有的键的关联值将保持不变）\n",
    "\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input('Provide a word to search for vowels:')\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter] += 1\n",
    "        for k,v in sorted(found.items()):\n",
    "            print(k,'was found',v,'time(s).')\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "46b3391c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'o', 'i', 'u', 'a', 'e'}\n",
      "{'o', 'i', 'u', 'a', 'e'}\n"
     ]
    }
   ],
   "source": [
    "# 从序创建集合\n",
    "vowels = {'a','e','i','o','u'}\n",
    "print(vowels)\n",
    "\n",
    "vowels2 = set('aeeiouu')\n",
    "print(vowels2)\n",
    "\n",
    "# 这两个完成的工作是一样的，它们都会向变量赋一个新的变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0e77c306",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "\n",
    "# union()方法 将一个集合与另一个集合合并\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a6da382e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " # difference()找出非共有元素\n",
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1841178e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# intersection()报告共同对象\n",
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af0f0b98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入：happt\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input('输入：')\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c007507",
   "metadata": {},
   "source": [
    "### 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f9f6f06b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "type(vowels)\n",
    "\n",
    "vowels2 = ('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dece9885",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4c02887b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "394003c9",
   "metadata": {},
   "source": [
    "#### 元组是不可变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "956bc60c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python')\n",
    "type(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d1d6efac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "84510da4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2 = ('Python',)\n",
    "type(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "526a8a6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Python',)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8d5caa6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只有一个对象的不是元组，每个元组在小括号之间至少要包含一个逗号\n",
    "# eg.t2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bc02d58",
   "metadata": {},
   "source": [
    "### 嵌套字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e3937c62",
   "metadata": {},
   "outputs": [],
   "source": [
    "people = {}\n",
    "people['Ford'] = {'name':'Lin Zhiyu',\n",
    "           'Gender':'Male',\n",
    "           'Occupation':'Researcher',\n",
    "           'Home Planet':'Betelgeuse Seven'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "049a8f76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Ford': {'name': 'Lin Zhiyu',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'}}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d6b56491",
   "metadata": {},
   "outputs": [],
   "source": [
    "people['Arthur'] = {'name':'YYK',\n",
    "           'Gender':'Male',\n",
    "           'Occupation':'Researcher',\n",
    "           'Home Planet':'Betelgeuse Seven'}\n",
    "people['zhanjie'] = {'name':'ZZY',\n",
    "           'Gender':'Male',\n",
    "           'Occupation':'Researcher',\n",
    "           'Home Planet':'Betelgeuse Seven'}\n",
    "people['lijie'] = {'name':'LJY',\n",
    "           'Gender':'Male',\n",
    "           'Occupation':'Researcher',\n",
    "           'Home Planet':'Betelgeuse Seven'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d70be814",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Ford': {'name': 'Lin Zhiyu',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'},\n",
       " 'Arthur': {'name': 'YYK',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'},\n",
       " 'zhanjie': {'name': 'ZZY',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'},\n",
       " 'lijie': {'name': 'LJY',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'}}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d4ad8d41",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pprint\n",
    "# 导入pprint模块，pprint（pretty print美观打印）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "eaf490c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Arthur': {'Gender': 'Male',\n",
      "            'Home Planet': 'Betelgeuse Seven',\n",
      "            'Occupation': 'Researcher',\n",
      "            'name': 'YYK'},\n",
      " 'Ford': {'Gender': 'Male',\n",
      "          'Home Planet': 'Betelgeuse Seven',\n",
      "          'Occupation': 'Researcher',\n",
      "          'name': 'Lin Zhiyu'},\n",
      " 'lijie': {'Gender': 'Male',\n",
      "           'Home Planet': 'Betelgeuse Seven',\n",
      "           'Occupation': 'Researcher',\n",
      "           'name': 'LJY'},\n",
      " 'zhanjie': {'Gender': 'Male',\n",
      "             'Home Planet': 'Betelgeuse Seven',\n",
      "             'Occupation': 'Researcher',\n",
      "             'name': 'ZZY'}}\n"
     ]
    }
   ],
   "source": [
    "pprint.pprint(people)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ece03fc2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
